package com.wangshb.agent.demo.runtime;

import com.wangshb.agent.demo.java.JvmStack;

import java.lang.instrument.Instrumentation;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 统计方法的运行时间
 *
 * @author wangsb12
 */
public class SimpleAgent {

    /**
     * 在main方法前运行
     * @param agentArgs
     * @param inst
     */
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println("hello, i'am agent!");
        inst.addTransformer(new MyTransformer());
        System.out.println("goodbye!");

        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                JvmStack.printMemoryInfo();
                JvmStack.printGCInfo();
            }
        }, 0, 5000, TimeUnit.MICROSECONDS);
    }
}
